package tech.mobi.child.setting;

import java.util.ArrayList;

import tech.mobi.child.MainAct;
import tech.mobi.child.base.BaseAct;
import tech.mobi.child.utils.Tools;
import tech.mobi.child.utils.UserInfo;

import tech.mobi.child.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

public class HelpMainAct extends BaseAct implements OnClickListener {

	private LayoutInflater inflater;
	private ViewPager viewPager;
	private ArrayList<View> pageViews;
//	private ImageView[] imageViews;
	// private String[] strArrayImage;
//	private String strTemp;

	private ImageView ivImage;
	private ImageView ivImageFlag1;
	private ImageView ivImageFlag2;
	private ImageView ivImageFlag3;
	private ImageView ivImageBtn;

	// 包裹滑动图片LinearLayout
	private ViewGroup main;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			this.requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.helpermain);

			inflater = getLayoutInflater();
			pageViews = new ArrayList<View>();
			main = (ViewGroup) inflater.inflate(R.layout.helpermain, null);

			View view = (View) inflater.inflate(R.layout.helper, null);
			ivImage = (ImageView) view.findViewById(R.id.ivImage);
			ivImageFlag1 = (ImageView) view.findViewById(R.id.ivImageFlag1);
			ivImageFlag2 = (ImageView) view.findViewById(R.id.ivImageFlag2);
			ivImageFlag3 = (ImageView) view.findViewById(R.id.ivImageFlag3);
			ivImageBtn = (ImageView) view.findViewById(R.id.ivImageBtn);

			ivImage.setBackgroundResource(R.drawable.help_first);
			ivImageFlag1.setBackgroundResource(R.drawable.help_flag_focused);
			ivImageFlag2.setBackgroundResource(R.drawable.help_flag);
			ivImageFlag3.setBackgroundResource(R.drawable.help_flag);
			
			ivImageBtn.setVisibility(View.INVISIBLE);
			pageViews.add(view);

			
			View view2 = (View) inflater.inflate(R.layout.helper, null);
			ivImage = (ImageView) view2.findViewById(R.id.ivImage);
			ivImageFlag1 = (ImageView) view2.findViewById(R.id.ivImageFlag1);
			ivImageFlag2 = (ImageView) view2.findViewById(R.id.ivImageFlag2);
			ivImageFlag3 = (ImageView) view2.findViewById(R.id.ivImageFlag3);
			ivImageBtn = (ImageView) view2.findViewById(R.id.ivImageBtn);
			
			ivImage.setBackgroundResource(R.drawable.help_second);
			ivImageFlag1.setBackgroundResource(R.drawable.help_flag);
			ivImageFlag2.setBackgroundResource(R.drawable.help_flag_focused);
			ivImageFlag3.setBackgroundResource(R.drawable.help_flag);
			
			ivImageBtn.setVisibility(View.INVISIBLE);
			pageViews.add(view2);

			
			View view3 = (View) inflater.inflate(R.layout.helper, null);
			ivImage = (ImageView) view3.findViewById(R.id.ivImage);
			ivImageFlag1 = (ImageView) view3.findViewById(R.id.ivImageFlag1);
			ivImageFlag2 = (ImageView) view3.findViewById(R.id.ivImageFlag2);
			ivImageFlag3 = (ImageView) view3.findViewById(R.id.ivImageFlag3);
			ivImageBtn = (ImageView) view3.findViewById(R.id.ivImageBtn);
			
			ivImage.setBackgroundResource(R.drawable.help_third);
			ivImageFlag1.setBackgroundResource(R.drawable.help_flag);
			ivImageFlag2.setBackgroundResource(R.drawable.help_flag);
			ivImageFlag3.setBackgroundResource(R.drawable.help_flag_focused);
			ivImageBtn.setOnClickListener(this);
			ivImageBtn.setVisibility(View.VISIBLE);
			pageViews.add(view3);

//			imageViews = new ImageView[pageViews.size()];

			viewPager = (ViewPager) main.findViewById(R.id.helperviewpager);
			setContentView(main);

			viewPager.setAdapter(new GuidePageAdapter());
			viewPager.setOnPageChangeListener(new GuidePageChangeListener());
		} catch (Exception e) {
			Log.i("MyLog", e.getMessage());
		}

	}

	// 指引页面数据适配器
	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub

		}
	}

	// 指引页面更改事件监听器
	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// int count = imageViews.length;
			// for (int i = 0; i < count; i++) {
			// // Log.i("MyLog", "onPageSelected:"+arg0 + "i:" + i);
			// int j = arg0 + 1;
			// tvPage.setText("" + j + "/" + count);
			// }
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ivImageBtn) {
			Intent intent = new Intent(this,MainAct.class);
			this.startActivity(intent);
			UserInfo.setVersion(this, Tools.getVersionName(this)
					.replace(".", ""));
			finish();
//			if(MainService.getActivityByName(HelpMainAct.class.getName()) != null)
//			{
//				MainService
//				.getActivityByName(HelpMainAct.class.getName()).finish();
//			}
			return;
		}

	}
	

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		
	}
}
